<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/6/1
 * Time: 下午1:55
 */

class Evaluation extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function checklogin()
    {
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['uid'])){
            $this->load->model('redis_model');
            $redis=$this->redis_model->getRedisInstance();
            $username = $redis->get('adv:evaluate:username:'.$_SESSION['uid']);
            if ($username===false) $this->success(array('status'=>1));
            else $this->success(array('status'=>2,'uid'=>$_SESSION['uid']));
        }else{
            $this->error('no session');
        }
    }
    public function firstlogin()
    {
        $username = $this->input->post('name');
        if (empty($username)||!isset($username)) $this->error('param error');

        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['uid'])){
            $this->load->model('redis_model');
            $redis=$this->redis_model->getRedisInstance();
            $redis->set('adv:evaluate:username:'.$_SESSION['uid'],$username);
            $this->success(array('uid'=>$_SESSION['uid']));
        }else{
            $this->error('no session');
        }
    }
    public function getFinishStatus()
    {
        $uid = $this->input->get('uid');
        $qstr = $this->input->get('qstr');
        if (!is_numeric($uid)) $this->error();

        if (!isset($qstr)||empty($qstr)) $qstr="0.1.2";
        $qdata = strpos($qstr,'.')===false?array($qstr):explode('.',$qstr);

        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();
        $redis->multi();
        foreach ($qdata as $q){
            $redis->getBit('adv:evaluation:complished:'.$q,$uid);
        }
        $data = $redis->exec();

        $res = array();
        $complishednum = 0;
        foreach ($qdata as $k=>$q){
            $resrow = array();
            $resrow['status']=(int)$data[$k];
            $complishednum+=$resrow['status'];
            switch ($q){
                case 0:
                    $resrow['title']="【看见自己，了解自己】测试一";
                    break;
                case 1:
                    $resrow['title']="【看见自己，了解自己】测试二";
                    break;
                case 2:
                    $resrow['title']="【你想要的幸福】测试";
                    break;
            }
            $resrow['testno']=$q;
            $res['complished'][] = $resrow;
        }

        $totalqnum = count($qdata);
        $res['num'] = $complishednum.'/'.$totalqnum;

        $this->success($res);
    }
    public function submit()
    {
        $post = $this->input->post();
        if (!is_numeric($post['uid'])||empty($post['data'])||!isset($post['data'])||!is_numeric($post['testno'])) $this->error('param error');
        $uid = (int)$post['uid'];
        $testno = (int)$post['testno'];
        $data = igbinary_serialize($post['data']);

        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();
        $redis->multi();
        $redis->setBit('adv:evaluation:complished:'.$testno,$uid,1);
        $redis->set('adv:evaluation:data:'.$testno.':'.$uid,$data);
        $redis->exec();
        $this->success();
    }
    public function getResults()
    {
        $uid = $this->input->get('uid');
        $qstr = $this->input->get('qstr');
        if (!is_numeric($uid)) $this->error();

        if (!isset($qstr)||empty($qstr)) $qstr="0.1.2";
        $qdata = strpos($qstr,'.')===false?array($qstr):explode('.',$qstr);

        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();

        $res = array();
        foreach ($qdata as $q){
            switch ((int)$q){
                case 0:
                    $test0 = $redis->get('adv:evaluation:data:0:'.$uid);
                    $test0score = array();
                    if ($test0!==false){
                        $test0 = igbinary_unserialize($test0);
                        foreach ($test0 as $ans){
                            switch ((int)$ans['type']){
                                case 1:
                                    $test0score['pmb']['score']+=(int)$ans['ans'];
                                    break;
                                case 2:
                                    $test0score['pmg']['score']+=(int)$ans['ans'];
                                    break;
                                case 3:
                                    $test0score['pvb']['score']+=(int)$ans['ans'];
                                    break;
                                case 4:
                                    $test0score['pvg']['score']+=(int)$ans['ans'];
                                    break;
                            }
                        }
                        $test0score['pmb']['title']='永久性的坏事(Permanent Bad)';
                        $test0score['pmg']['title']='永久性的好事(Permanent Good)';
                        $test0score['pvb']['title']='普遍性的坏事(Pervasiveness Bad)';
                        $test0score['pvg']['title']='普遍性的好事(Pervasiveness Good)';
                        $test0score['hob']['title']='HoB分数';
                        $test0score['hob']['score']=$test0score['pmb']['score']+$test0score['pvb']['score'];
                        $test0score['hog']['title']='HoG分数';
                        $test0score['hog']['score']=$test0score['pmg']['score']+$test0score['pvg']['score'];
                        $test0score['hog-hob']['title']='HoG-HoB分数';
                        $test0score['hog-hob']['score']=$test0score['hog']['score']-$test0score['hob']['score'];
                    }
                    $res['test0']=array_values($test0score);
                    break;
                case 1:
                    $test1 = $redis->get('adv:evaluation:data:1:'.$uid);
                    $test1score = array();
                    if ($test1!==false){
                        $test1 = igbinary_unserialize($test1);
                        foreach ($test1 as $ans){
                            $test1score['score'] += $ans['ans'];
                        }
                        $test1score['title']='总得分';
                    }
                    $res['test1']=array_values($test1score);
                    break;
                case 2:
                    $test2 = $redis->get('adv:evaluation:data:2:'.$uid);
                    $test2score = array();
                    if ($test2!==false){
                        $test2 = igbinary_unserialize($test2);
                        foreach ($test2 as $ans){
                            switch ((int)$ans['type']){
                                case 1:
                                    $test2score['happy']['score']+=(int)$ans['ans'];
                                    break;
                                case 2:
                                    $test2score['input']['score']+=(int)$ans['ans'];
                                    break;
                                case 3:
                                    $test2score['meanning']['score']+=(int)$ans['ans'];
                                    break;
                                case 4:
                                    $test2score['victory']['score']+=(int)$ans['ans'];
                                    break;
                            }
                        }
                        $test2score['happy']['title']='追求快乐取向';
                        $test2score['input']['title']='投入取向';
                        $test2score['meanning']['title']='追求意义取向';
                        $test2score['victory']['title']='胜利取向';
                    }
                    $res['test2']=array_values($test2score);
                    break;
            }
        }

        $this->success($res);
    }

}